package com.heer.gymnasium.module.coach.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.ImageView;

import com.amap.api.maps.model.Text;
import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.SignHelper;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.api.constants.IConstants;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.map.views.MapShowActivity;
import com.heer.imagepick.DensityUtils;
import com.zbar.lib.encoding.EncodingUtils;
import com.zhihuiyun.library.utils.BitmapUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/3/8
 * 私教订课
 */

public class CoachCourseActivity extends BaseActivity {

    private static final int RES_ID_LAYOUT = R.layout.layout_coach_course;
    private static final int RES_ID_IV = R.id.layout_coach_course_iv;

    private String encode = "123";

    private Activity $this;

    @BindView(RES_ID_IV)
    ImageView imageView;

    public static void startActvity(Activity activity) {
        Intent intent = new Intent(activity, CoachCourseActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_LAYOUT);
    }

    @Override
    protected void initView() {
        super.initView();
        getCoachId();
    }

    // 获取私教Id
    private void getCoachId() {
        SignHelper signHelper = new SignHelper("");
        mAppHelper.apiService().getCoachId(signHelper.getToken()).enqueue(new BaseCallback<ApiResponse<String>>() {
            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                encode = IConstants.BASRURL + "/dist/coachDetail?id=" + response.body().getData();
                if (!TextUtils.isEmpty(encode)) {
                    Bitmap bitmap = EncodingUtils.createQRCode(encode , DensityUtils.dip2px($this, 250),
                            DensityUtils.dip2px($this, 250), BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                    imageView.setImageBitmap(bitmap);
                } else {
                    ToastUtil.show($this, "无私教订课信息");
                    finish();
                }
            }

            @Override
            public void onError(Call<ApiResponse<String>> call, Throwable t) {
                super.onError(call, t);
                finish();
            }
        });
    }
}
